﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP
{
    public partial class ShippingDetail : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            String ReferenceNumber = Session["ReferenceNumber"].ToString();
            String TotalOrder= Session["TotalOrder"].ToString();

            Label4.Text = "<input type='hidden' name='item_name' value=" + ReferenceNumber + " /><input type='hidden' name='amount' value=" + TotalOrder +"/>";
        
            if (Session["Username"] != null && Session["ID"] != null)
            {
                Panel1.Visible = true;
                lblUserName.Text = Session["Username"].ToString();
                lblUserID.Text = Session["ID"].ToString();
                lblUserID.Visible = false;
            }
            else
            {
                Panel1.Visible = false;
            }

            if (!IsPostBack)
            {
                try
                {
                    SqlCommand cmdUser;
                    SqlDataReader dtrUser;
                    conn.Open();

                    cmdUser = new SqlCommand("Select * From Users where u_id = @ID", conn);
                    cmdUser.Parameters.AddWithValue("@ID",Session["ID"].ToString());
                    dtrUser = cmdUser.ExecuteReader();
                    dtrUser.Read();

                   
                    String userName = dtrUser["username"].ToString();
                    String password = dtrUser["password"].ToString();
                    String IC = dtrUser["u_ic"].ToString();
                    String age = dtrUser["u_age"].ToString();
                    String gender = dtrUser["u_gender"].ToString();
                    String address = dtrUser["u_address"].ToString();
                    string addressBeforeSplit = dtrUser["u_address"].ToString();

                    string[] lines = addressBeforeSplit.Split('|');
                    String phone = dtrUser["u_phone"].ToString();
                    String email = dtrUser["u_email"].ToString();


                    txtName.Text = userName;
                    txtAdd1.Text = lines[0].ToString();
                    txtAdd2.Text = lines[1].ToString();
                    txtPostCode.Text = lines[2].ToString();
                    txtCity.Text = lines[3].ToString();
                    txtState.Text = lines[4].ToString();
                    txtCountry.Text = lines[5].ToString();
                    txtPhone.Text = phone;
                    txtEmail.Text = email;


                    txtName.Enabled = false;
                    txtAdd1.Enabled = false;
                    txtAdd2.Enabled = false;
                    txtPostCode.Enabled = false;
                    txtCity.Enabled = false;
                    txtState.Enabled = false;
                    txtCountry.Enabled = false;
                    txtPhone.Enabled = false;
                    txtEmail.Enabled = false;
          
                    dtrUser.Close();
                    conn.Close();
                }
                catch (SqlException ex)
                {

                }
            }

        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (RadioButtonList1.SelectedIndex == 0)
            {
                Label3.Visible = false;
                PanelBilling.Visible = false;
            }
            else
            {
                PanelBilling.Visible = true;
                Label3.Visible = true;
  
            }
        }

        protected void btnDelivery_Click(object sender, EventArgs e)
        {
            if (PanelDelivery.Visible == true && PanelBilling.Visible == false)
            {

                try
                {

                    String fullAddress = txtAdd1.Text + "|" + txtAdd2.Text + "|" + txtPostCode.Text + "|" + txtCity.Text + "|" + txtState.Text + "|" + txtCountry.Text;
                    String Email = txtEmail.Text;
                    String PhoneNo = txtPhone.Text;
                    String Recipient = txtName.Text;
                    conn.Open();


                    SqlCommand cmdSearchID = new SqlCommand("Select MAX(s_id) from Shipping_Details", conn);
                    int s_id = Convert.ToInt32(cmdSearchID.ExecuteScalar());
                    s_id++;

                    SqlCommand cmdInsert1 = new SqlCommand("Insert Into Shipping_Details Values(@SID,@Sphone,@Recipient,@Address,@Email)", conn);

                    cmdInsert1.Parameters.AddWithValue("@SID", Convert.ToInt32(s_id));
                    cmdInsert1.Parameters.AddWithValue("@Sphone", Convert.ToString(PhoneNo));
                    cmdInsert1.Parameters.AddWithValue("@Recipient", Convert.ToString(Recipient));
                    cmdInsert1.Parameters.AddWithValue("@Address", Convert.ToString(fullAddress));
                    cmdInsert1.Parameters.AddWithValue("@Email", Convert.ToString(Email));
                    cmdInsert1.ExecuteNonQuery();




                    SqlCommand cmdUpdate1 = new SqlCommand("Update Transactions SET s_id=@SID Where t_status=@STATUS and u_id=@UID", conn);

                    cmdUpdate1.Parameters.AddWithValue("@SID", Convert.ToInt32(s_id));
                    cmdUpdate1.Parameters.AddWithValue("@STATUS", "Cart");
                    cmdUpdate1.Parameters.AddWithValue("@UID", Session["ID"].ToString());

                    cmdUpdate1.ExecuteNonQuery();

                    conn.Close();
                }
                catch (SqlException ex)
                {
                    Response.Write(ex.Message + "");
                }

            }
            else
            {
                try
                {

                    String fullAddress = txtOptionAdd1.Text + "|" + txtOptionAdd2.Text + "|" + txtOptionPostCode.Text + "|" + txtOptionCity.Text + "|" + txtOptionState.Text + "|" + txtOptionCountry.Text;
                    String Email = txtOptionEmail.Text;
                    String PhoneNo = txtOptionPhone.Text;
                    String Recipient = txtOptionName.Text;
                    conn.Open();


                    SqlCommand cmdSearchID = new SqlCommand("Select MAX(s_id) from Shipping_Details", conn);
                    int s_id = Convert.ToInt32(cmdSearchID.ExecuteScalar());
                    s_id++;

                    SqlCommand cmdInsert1 = new SqlCommand("Insert Into Shipping_Details Values(@SID,@Sphone,@Recipient,@Address,@Email)", conn);

                    cmdInsert1.Parameters.AddWithValue("@SID", Convert.ToInt32(s_id));
                    cmdInsert1.Parameters.AddWithValue("@Sphone", Convert.ToString(PhoneNo));
                    cmdInsert1.Parameters.AddWithValue("@Recipient", Convert.ToString(Recipient));
                    cmdInsert1.Parameters.AddWithValue("@Address", Convert.ToString(fullAddress));
                    cmdInsert1.Parameters.AddWithValue("@Email", Convert.ToString(Email));
                    cmdInsert1.ExecuteNonQuery();




                    SqlCommand cmdUpdate1 = new SqlCommand("Update Transactions SET s_id=@SID Where t_status=@STATUS and u_id=@UID", conn);

                    cmdUpdate1.Parameters.AddWithValue("@SID", Convert.ToInt32(s_id));
                    cmdUpdate1.Parameters.AddWithValue("@STATUS", "Cart");
                    cmdUpdate1.Parameters.AddWithValue("@UID", Convert.ToInt32("1"));

                    cmdUpdate1.ExecuteNonQuery();

                    conn.Close();
                }
                catch (SqlException ex)
                {
                    Response.Write(ex.Message + "");
                }




            }
        }
    }
}